//
//  Auction_Main_CollectionView_WX.m
//  ZITHER
//
//  Created by weixin on 2019/5/27.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "Auction_Main_CollectionView_WX.h"
#import "Auction_Main_Cell_WX.h"
#import "Auction_Main_CellHeader_WX.h"
#import "Auction_Main_CellFooter_WX.h"
#import "Auction_MainRecommend_Cell_WX.h"
#import "Auction_SpecialSaleDetails_VC_WX.h"

#define CELL_COUNT 30

@interface Auction_Main_CollectionView_WX ()

@property (nonatomic, strong) NSArray *cellSizes;

@property (nonatomic, strong) NSArray *cats;

@end

@implementation Auction_Main_CollectionView_WX

static NSString *const ID = @"cell";


+(CHTCollectionViewWaterfallLayout*)GetLayyout{
    CHTCollectionViewWaterfallLayout *layout = [[CHTCollectionViewWaterfallLayout alloc] init];
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
//    layout.headerHeight = 70+(kScreenW-30)/344.0*277*3;
//    layout.footerHeight = 0.01;
    layout.minimumColumnSpacing = 10;
    layout.minimumInteritemSpacing = 10;
    return layout;
}


- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout{
    self = [super initWithFrame:frame collectionViewLayout:layout];
    if (self) {
        self.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
        self.dataSource = self;
        self.delegate = self;
        self.backgroundColor = HexColor(0xeeeeee);
        [self registerClass:[Auction_Main_Cell_WX class]
 forCellWithReuseIdentifier:@"Auction_Main_Cell_WX"];
        [self registerClass:[Auction_MainRecommend_Cell_WX class]
 forCellWithReuseIdentifier:@"Auction_MainRecommend_Cell_WX"];
                
        [self registerClass:[Auction_Main_CellHeader_WX class]
 forSupplementaryViewOfKind:CHTCollectionElementKindSectionHeader
        withReuseIdentifier:@"Auction_Main_CellHeader_WX"];
        
        [self registerClass:[Auction_Main_CellFooter_WX class]
 forSupplementaryViewOfKind:CHTCollectionElementKindSectionFooter
        withReuseIdentifier:@"Auction_Main_CellFooter_WX"];
        
    }
    return self;
}


#pragma mark - UICollectionViewDataSource
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger index = self.model.list.count-self.model.recommendCount;
    specialSaleSceneidList * model = self.model.list[index];
    Auction_SpecialSaleDetails_VC_WX *vc = [[Auction_SpecialSaleDetails_VC_WX alloc]init];
    vc.type = [model.type isEqualToString:@"QUOTE"]?1:0;
    vc.DetailsId = model.id;
    [[self getNav]pushViewController:vc animated:YES];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.model.list.count-self.model.recommendCount;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    return [self getAuction_MainRecommend_Cell_WX:indexPath];
    
//    specialSaleSceneidList *model = self.model.list[indexPath.row-self.model.recommendCount];
//    if (indexPath.row < 3) {
//    }else{
//        Auction_Main_Cell_WX *cell =
//        (Auction_Main_Cell_WX *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Auction_Main_Cell_WX"
//                                                                          forIndexPath:indexPath];
//        return cell;
//    }
}

-(Auction_MainRecommend_Cell_WX *)getAuction_MainRecommend_Cell_WX:(NSIndexPath *)indexPath{
    specialSaleSceneidList *model = self.model.list[indexPath.row];
    Auction_MainRecommend_Cell_WX *cell =
    (Auction_MainRecommend_Cell_WX *)[self dequeueReusableCellWithReuseIdentifier:@"Auction_MainRecommend_Cell_WX"
                                                                      forIndexPath:indexPath];
    cell.model = model;
    return cell;
}



- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath {
    UICollectionReusableView *reusableView = nil;
    
    if ([kind isEqualToString:CHTCollectionElementKindSectionHeader]) {
        Auction_Main_CellHeader_WX *head = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                                             withReuseIdentifier:@"Auction_Main_CellHeader_WX"
                                                                                    forIndexPath:indexPath];
        head.model = self.model;
        reusableView = head;
        
    }else{
        reusableView = [collectionView dequeueReusableSupplementaryViewOfKind:kind
                                                          withReuseIdentifier:@"Auction_Main_CellFooter_WX"
                                                                 forIndexPath:indexPath];
    }
    return reusableView;
}


#pragma mark - CHTCollectionViewDelegateWaterfallLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    specialSaleSceneidList *model = self.model.list[indexPath.row];
    CGFloat cellWidth = (kScreenW-30)/2.0;
    if (model.heightCell == 0) {
        CGFloat imgH =  ((kScreenW-30)/2.0/168.0)*198.0+50;
        if (Is_Iphone_X_Series) {
            model.heightCell = 240+ [NSString textFontHeight:model.title font:[UIFont fontWithName:@"PingFangSC-Semibold" size:12] width:kScreenW/2.0-20];
        }else{
            model.heightCell = 210+ [NSString textFontHeight:model.title font:[UIFont fontWithName:@"PingFangSC-Semibold" size:12] width:kScreenW/2.0-20];
        }
    }

    return CGSizeMake(168, model.heightCell);

//    return CGSizeMake(kScreenW-30/2.0,300);
//
//    specialSaleSceneidList *model = self.model.list[indexPath.row];
//
//    if (model.heightCell == 0) {
//        CGFloat imgH =  ((kScreenW-30)/2.0/168.0)*198.0+50;
//        model.heightCell = imgH + [NSString textFontHeight:model.title font:[UIFont fontWithName:@"PingFangSC-Semibold" size:12] width:kScreenW/2.0-20];
//    }
//
//    return CGSizeMake(kScreenW-30/2.0, model.heightCell);

//    if (indexPath.row%3 == 1) {
//        return CGSizeMake(168, 240);
//    }else{
//        return CGSizeMake(168, 260);
//    }
}

- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout heightForHeaderInSection:(NSInteger)section{
    if (self.model.recommendCount == 0 ) {
        return 30;
    }else{
        return 70+(kScreenW-30)/344.0*277*self.model.recommendCount;
    }
}


@end
